/*
 * 创建日期 2005-12-12
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.dp;

import java.lang.reflect.Method;
import java.util.List;

import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;
import com.vanda.report.Validation;
import com.vanda.report.cfg.ReportCfgBean;
import com.vanda.report.cfg.ValidatorBean;
import com.vanda.report.element.BeanElement;

/**
 * @author wkf5985
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class ValidatorElement extends BeanElement{
	
	private String depends;
	private String check_property;
	private String msg_key;
	
	public void validator() throws ReportException{
		Validation.checkRequired("ValidatorElement","property",super.getProperty());
		Validation.checkRequired("ValidatorElement","depends",depends);
		Validation.checkRequired("ValidatorElement","check_property",check_property);
	}
	
	/**
	 * 标签执行方法，递归执行
	 * 临时变量说明:
	 * temp1:原始数据list
	 * temp2:当前行数
	 * temp3:Object[]数组,0-List(保存顺序号) ,1-HashMap(key-顺序号,value-需要新插入的值)
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException{
		ReportInnerData reportData = (ReportInnerData)obj;
		String property = super.getProperty();
		
		try{
			//取得需要校验的数据
			Object validatorObj = super.getListObject(reportData, property);
			//取得校验类型
			ValidatorBean vBean = ReportCfgBean.getValidatorBean(depends);
			//取得错误信息
			List errorList = reportData.getErrors();
			
			if(validatorObj instanceof List){
				List lstObj = (List)validatorObj;
				for(int i=lstObj.size()-1;i>=0;i--){
					Object temp = lstObj.get(i);
					
					//校验是否正确
					boolean bValidator = validator(validatorObj,temp,vBean,errorList);
					
					//如果校验失败,则运行子元素
					if(!bValidator){
						//设置错误信息
						String errorMsg = vBean.getMsg(temp,msg_key);
						if(!errorList.contains(errorMsg)){
							errorList.add(errorMsg);
						}
						reportData.setContextData(temp);
						super.execute(obj);
					}
				}
			}else{
				//校验是否正确
				boolean bValidator = validator(validatorObj,validatorObj,vBean,errorList);
					
				//如果校验失败,则运行子元素
				if(!bValidator){
					//设置错误信息
					String errorMsg = vBean.getMsg(validatorObj,msg_key);
					if(!errorList.contains(errorMsg)){
						errorList.add(errorMsg);
					}
					reportData.setContextData(validatorObj);
					super.execute(obj);
				}
			}

		}catch(ReportException e){
			throw e;
		}catch(Exception e){
			throw new ReportException(e);
		}

	}
	
	private boolean validator(Object validatorObj,Object singleObj,ValidatorBean vBean,List errorList) throws Exception{
		Object validatorCheck = Class.forName(vBean.getClassName()).newInstance();
		Class[] cls = new Class[3];
		cls[0] = Object.class;
		cls[1] = Object.class;
		cls[2] = String.class;
		Method checkMethod = validatorCheck.getClass().getMethod(vBean.getMethod(),cls);
		
		Object[] params = new Object[3];
		params[0] = validatorObj;
		params[1] = singleObj;
		params[2] = this.getCheck_property();
		//执行方法
		Boolean bCheck = (Boolean)checkMethod.invoke(validatorCheck,params);
		
		
		return bCheck.booleanValue();
	}
	
	/**
	 * @return
	 */
	public String getDepends() {
		return depends;
	}

	/**
	 * @param string
	 */
	public void setDepends(String string) {
		depends = string;
	}


	/**
	 * @return
	 */
	public String getCheck_property() {
		return check_property;
	}

	/**
	 * @return
	 */
	public String getMsg_key() {
		return msg_key;
	}

	/**
	 * @param string
	 */
	public void setCheck_property(String string) {
		check_property = string;
	}

	/**
	 * @param string
	 */
	public void setMsg_key(String string) {
		msg_key = string;
	}

}
